import { _decorator, Component, Node, Animation, Label, Game } from 'cc';
import { GameManager } from './GameManager';
import { GameUtil } from './GameUtil';
import { AWARYTYPE } from '../AshFramework/Datas/GameConstant';
import { MenuManager } from './MenuManager';
const { ccclass, property } = _decorator;

@ccclass('Task')
export class Task extends Component {

    @property(Node)
    TaskBtnNode: Node = null;

    @property(Node)
    TaskNode: Node = null;

    @property(Label)
    CurNum: Label = null;//当前天任务的次数

    @property(Label)
    TaskLabel: Label = null;//任务内容

    @property(Label)
    CurProgress: Label = null;//当前任务的进度

    @property(Label)
    TotalProgress: Label = null;//总的任务要求


    @property(Label)
    AwardMasonry: Label = null;//任务奖励

    taskBtn() {
        this.TaskBtnNode.active = false;
        this.TaskNode.active = true;
        this.TaskNode.getComponent(Animation).play();
        this.taskFinish();
    }

    taskBackBtn() {
        this.TaskBtnNode.active = true;
        this.TaskNode.active = false;
    }

    showTask() {
        if (GameManager.Instance.userData.taskNum > 5) {
            this.TaskBtnNode.active = false;
            return;
        }
        this.CurNum.string = String(GameManager.Instance.userData.taskNum);
        this.TaskLabel.string = GameManager.Instance.userData.task.label;
        this.CurProgress.string = GameUtil.formatNumber(GameManager.Instance.userData.task.curProgress);
        this.TotalProgress.string = GameUtil.formatNumber(GameManager.Instance.userData.task.totalProgress);
        this.AwardMasonry.string = GameUtil.formatNumber(GameManager.Instance.userData.task.award);
    }

    taskFinish() {
        if (GameManager.Instance.userData.taskNum > 5) {
            this.TaskBtnNode.active = false;
            return;
        }
        if (GameManager.Instance.userData.task.curProgress >= GameManager.Instance.userData.task.totalProgress) {
            GameUtil.getAward(AWARYTYPE.MASONRY, MenuManager.Instance.getMasonryWorldPos(), () => {
                GameManager.Instance.userData.taskNum++;
                MenuManager.Instance.addMasonry(GameManager.Instance.userData.task.award);
                GameManager.Instance.userData.task = GameUtil.getTask(GameManager.Instance.userData.lv, GameManager.Instance.userData.taskNum);
                this.showTask();
            })
        }
    }

    protected onEnable(): void {
        this.showTask();
        this.TaskBtnNode.active = true;
        this.TaskNode.active = false;

    }

}


